<?php

namespace app\store\service;

use app\store\model\IntegralGoods;
use think\facade\Db;

class IntegralGoodsService
{
    public function getIntegralGoodsList($param)
    {   
        $store_id = session('store_id');

        $query = Db::name('integral_goods')
                    ->field('a.id,a.sort,a.redeem_price,a.redeem_integral,a.redeem_stock,a.redeem_num,a.status,a.create_time,a.update_time,
                           b.goods_name,b.main_image,b.sell_price')
                    ->alias('a')
                    ->join('goods b','a.goods_id=b.id')
                    ->where('a.store_id',$store_id);


        if(isset($param['status']) and $param['status'] != ''){
            $query->where('a.status',$param['status']);
        }

        if(!empty($param['keyword'])){
            $query->whereLike('b.goods_name',"%{$param['keyword']}%");
        }

        $goods = $query->order('a.id','desc')->paginate(10);

        return $goods;
    }


    public function findIntegralGoods($id)
    {
        $model = new IntegralGoods();
        $goods = $model->withOutField('create_time,update_time,delete_time')
                        ->with(['goods','goodsItems','items'])
                        ->where('id',$id)
                        ->find();

        $arr =  $goods->toArray();
        $old = $arr['goodsItems'];
        $new = $arr['items'];
        $item = [];
        foreach ($new as $k => $v){
            $item[$k]['attr'] = $old[$k]['attr'];
            $item[$k]['image'] = $old[$k]['image'];
            $item[$k]['sell_price'] = $old[$k]['sell_price'];
            $item[$k]['id'] = $v['id'];
            $item[$k]['sku_id'] = $v['sku_id'];
            $item[$k]['redeem_price'] = $v['redeem_price'];
            $item[$k]['redeem_integral'] = $v['redeem_integral'];
            $item[$k]['redeem_stock'] = $v['redeem_stock'];
        }

        $goods['items'] = $item;

        return $goods;
    }

    /**
     * 添加积分商品
     * */
    public function saveIntegralGoods($data)
    {
        $data['store_id'] = session('store_id');

        if ($data['spec_type']==2){
            $skus = $this->formatGoodsInfo($data);
            $goods = IntegralGoods::create($data);
            $goods->items()->saveAll($skus);

            return $goods['id'];
        }

        $goods = IntegralGoods::create($data);

        return $goods['id'];
    }

    /**
     * 更新积分商品
     * */
    public function updateIntegralGoods($data)
    {
        if ($data['spec_type']==2){
            $skus = $this->formatGoodsInfo($data,true);
            $goods = IntegralGoods::update($data);
            //halt($skus);
            $goods->items()->saveAll($skus);
            return $goods['id'];
        }

        $goods = IntegralGoods::update($data);
        return $goods['id'];
    }

    /**
     * 格式化商品信息
     * */
    protected function formatGoodsInfo(&$data,$isUpdate = false)
    {
        $arr = json_decode($data['skus'], true);
        $skus = [];

        if ($isUpdate){
            foreach ($arr as $k => $v){
                $skus[$k]['id'] = $v['id'];
                $skus[$k]['sku_id'] = $v['sku_id'];
                $skus[$k]['redeem_stock'] = $v['redeem_stock'];
                $skus[$k]['redeem_price'] = $v['redeem_price'];
                $skus[$k]['redeem_integral'] = $v['redeem_integral'];
            }
        } else {
            foreach ($arr as $k => $v){
                $skus[$k]['sku_id'] = $v['id'];
                $skus[$k]['redeem_stock'] = $v['redeem_stock'];
                $skus[$k]['redeem_price'] = $v['redeem_price'];
                $skus[$k]['redeem_integral'] = $v['redeem_integral'];
            }
        }

        $data['redeem_price'] = min(array_column($skus,'redeem_price'));
        $data['redeem_integral'] = min(array_column($skus,'redeem_integral'));
        $data['redeem_stock'] = array_sum(array_column($skus,'redeem_stock'));

        return $skus;
    }


}